<?php
error_reporting(7);
$DB_SERVER = "localhost"; 	/* database server hostname */
$DB_NAME = "workflow"; 		/* database name */
$DB_USER = "root"; 			/* database user */
$DB_PASS = ""; 				/* database password */
$DB_SELECT_DB = "";
$SESS_LIFE = get_cfg_var("session.gc_maxlifetime");   // $SESS_LIFE = 1440;
function sess_open($save_path, $session_name) {
	global $DB_SERVER, $DB_NAME, $DB_USER, $DB_PASS, $DB_SELECT_DB;

	if (! $DB_SELECT_DB = mysql_pconnect($DB_SERVER, $DB_USER, $DB_PASS)) {
		echo "SORRY! MYSQL ERROR : Can't connect to $DB_SERVER as $DB_USER";
		echo "MySQL Error: ", mysql_error();
		die;
	}

	if (! mysql_select_db($DB_NAME, $DB_SELECT_DB)) {
		echo "SORRY! MYSQL ERROR : Unable to select database $DB_NAME";
		die;
	}
	sess_gc(0);
	return true;
}

function sess_close() {
	return true;
}

function sess_read($key) {

	global $DB_SELECT_DB, $SESS_LIFE, $db_prefix, $session, $onlineuser;
	$session = $DB->fetch_one_array("SELECT * FROM " . $db_prefix . "session WHERE sessionid='$key'");
	if (!empty($session) AND $session[expiry] > time()) {
		return $session[value];
	} else {
		$onlineuser++;
		return "";
	}
}

function sess_write($key, $val) {
	global $DB_SELECT_DB, $db_prefix, $SESS_LIFE, $pauserinfo, $session;
	$expiry = time() + $SESS_LIFE;
	$value = addslashes($val);

	$ipaddress = getip();
	$useragent = $_SERVER[HTTP_USER_AGENT];
	$REQUEST_URI = $_SERVER[REQUEST_URI];

	if (empty($session) AND $key != $session['key']) {
		$sql = "INSERT IGNORE INTO " . $db_prefix 
			. "session (sessionid,expiry,value,userid,ipaddress,useragent,location,lastactivity)" 
			. "VALUES ('$key',$expiry,'$value','$pauserinfo[userid]','" . addslashes($ipaddress) 
			. "','" . addslashes($useragent) . "','" . addslashes($REQUEST_URI) . "','" . time() . "')";
		$query = mysql_query($sql, $DB_SELECT_DB);
	} else {
		$sql = "UPDATE " . $db_prefix 
				. "session SET
                         expiry='$expiry',
                         userid='$pauserinfo[userid]',
                         value='$value',
                         ipaddress='" . addslashes($ipaddress) . "',
                         useragent='" . addslashes($useragent) . "',
                         location='" . addslashes($REQUEST_URI) . "',
                         lastactivity='" . time() . "'
                         WHERE sessionid='$key'";
		$query = mysql_query($sql, $DB_SELECT_DB);
	}
	if ($pauserinfo['userid'] != 0) {
		if ((time() - $pauserinfo['lastactivity']) > $SESS_LIFE) {
			mysql_query("UPDATE " . $db_prefix 
				. "user SET lastvisit=lastactivity,lastactivity='" . time() 
				. "' WHERE userid='$pauserinfo[userid]'", $DB_SELECT_DB);
		} else {
			mysql_query("UPDATE " . $db_prefix 
				. "user SET lastactivity='" . time() 
				. "' WHERE userid='$pauserinfo[userid]'", $DB_SELECT_DB);
		}
	}
	return $query;
}

function sess_destroy($key) {
	global $DB_SELECT_DB, $db_prefix;
	return $DB->query("DELETE FROM " . $db_prefix . "session WHERE sessionid='$key'");
}

function sess_gc($maxlifetime) {
	global $DB_SELECT_DB, $db_prefix;
	$query = $DB->query("DELETE FROM " . $db_prefix . "session WHERE expiry<" . time() . "");
	return $DB->affected_rows();
}

session_set_save_handler("sess_open",
"sess_close",
"sess_read",
"sess_write",
"sess_destroy",
"sess_gc");

@session_start();
?>